<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="店铺">
              <!--              <j-dict-select-tag placeholder="请选择店铺" v-model="queryParam.shopId" dictCode="mk_shop_basic,name,id"/>-->
              <a-select type="list_multi" v-model="queryParam.shopId" placeholder="请选择店铺">
                <a-select-option v-for="(i, k) in shopList" :value="i.id" :key="i.id">
                  {{ i.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="订单号">
              <a-input placeholder="输入订单号查询" v-model="queryParam.orderNo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="请选择订单状态">
              <j-dict-select-tag placeholder="请选择订单状态" v-model="queryParam.status" dict-code="order_status">
              </j-dict-select-tag>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="请选择订单类型">
              <j-dict-select-tag placeholder="请选择订单类型" v-model="queryParam.type" dict-code="order_type">
              </j-dict-select-tag>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!--  <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('商品订单信息表')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>-->
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery">
      </j-super-query>

      <!-- <a-button type="primary" @click="showModal" v-has="'order:verifyOrder'"> 核销订单 </a-button> -->

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
          <a-menu-item key="2" v-has="'order:forceCompletion'" @click="forceComplete"> 强制完成 </a-menu-item>
          <a-menu-item key="3" v-has="'order:forceRefund'" @click="forceRefund"> 强制退款 </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>
    <a-modal :centered="true" title="核销订单" :visible="visibleVer" @ok="handleOk(verifyCode)" @cancel="handleCancel">
      <a-input style="width: 80%" v-model="verifyCode" placeholder="请输入核销码" allow-clear></a-input>
    </a-modal>
    <a-modal :centered="true" title="强制完成所选订单" :visible="showForcedOk" @ok="forceOk" @cancel="handleCancel">
      <a-textarea placeholder="请输入备注信息" v-model="okRemark" :rows="4" style="resize: none; width: 100%" />
    </a-modal>
    <a-modal :centered="true" title="所选订单强制退款" :visible="showForcedRefund" @ok="forceRefundOk" @cancel="handleCancel">
      <a-textarea placeholder="请输入备注信息" v-model="refundRemark" :rows="4" style="resize: none; width: 100%" />
    </a-modal>
    <!-- table区域-begin -->
    <div>
      <div>如需核销订单，请直接点击核销按钮，输入一个核销码进行核销，无需勾选订单</div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{ x: true }" bordered rowKey="id" :columns="columns" :dataSource="dataSource" :pagination="ipagination" :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" class="j-table-force-nowrap" @change="handleTableChange">
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width: 80px; font-size: 12px; font-style: italic" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">详情</a>
          <a-divider type="vertical" v-if="record.status === '1'&&record.type=='2'" /> 
          <a @click="showm(record)" v-if="record.status === '1'&&record.type=='2'">发货</a>

          <!--<a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>-->
        </span>
      </a-table>
    </div>
    <j-modal title="订单发货" :width="width" :visible="goods" switchFullscreen @ok="shippedOk" @cancel="goods=false" cancelText="关闭">
      <a-form-model>
        <a-row>
          <a-col :span="24/2">
            <a-form-model-item label="分站" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="siteId">
              <j-dict-select-tag disabled placeholder="请输入分站" v-model="model.siteId" dict-code="mk_site_basic,name,id">
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-model-item label="商家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="merchantId">
              <!--              <a-input v-model="model.merchantId" placeholder="请输入商家"></a-input>-->
              <j-dict-select-tag disabled placeholder="请输入商家" v-model="model.merchantId" dict-code="mk_merchant_basic,name,id">
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-model-item label="店铺" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopId">
              <!--              <a-input v-model="model.shopId" placeholder="请输入店铺"></a-input>-->
              <j-dict-select-tag disabled placeholder="请输入店铺" v-model="model.shopId" dict-code="mk_shop_basic,name,id">
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-model-item label="订单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNo">
              <a-input disabled v-model="model.orderNo" placeholder="请输入订单号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-model-item label="下单会员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="memberId">
              <j-dict-select-tag disabled placeholder="请输入会员" v-model="model.memberId" dict-code="mk_member_basic,nick_name,id">
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24/2">
            <a-form-model-item label="会员联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="memberId">
              <j-dict-select-tag disabled placeholder="请输入会员联系方式" v-model="model.memberId" dict-code="mk_member_basic,mobile,id">
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
        </a-row>
        <!-- 子表单区域 -->
        <a-tabs defaultActiveKey="1">
          <a-tab-pane tab="商品订单行信息" key="1" forceRender>
            <div>
              <a-row type="flex" style="margin-bottom:10px" :gutter="16" v-for="(item, index) in model.orderLineList" :key="index">
                <a-col :span="5">
                  <a-form-model-item>
                    <a-input disabled placeholder="订单号" v-model="item.orderNo" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="6">
                  <a-form-model-item>
                    <a-input disabled placeholder="商品名称" v-model="item.goodsName" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="6">
                  <a-form-model-item>
                    <a-input disabled placeholder="所选规格" v-model="item.nameStr" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="2">
                  <a-form-model-item>
                    <a-input disabled placeholder="数量" v-model="item.quantity" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="2">
                  <a-form-model-item>
                    <a-input disabled placeholder="合计价格" v-model="item.totalPrice" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="2">
                  <a-form-model-item>
                    <a-input disabled placeholder="商品备注" v-model="item.remark" />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </div>
          </a-tab-pane>
          <a-tab-pane tab="商品订单配送信息" key="2">
            <a-row type="flex" style="margin-bottom:10px" :gutter="16">
              <a-col :span="5">
                <a-form-model-item>
                  <j-dict-select-tag type="list" v-model="shipped.expressCompany" dictCode="express_company" placeholder="请选择快递公司" />
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item>
                  <a-input placeholder="快递单号" v-model="shipped.expressNo" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-tab-pane>
        </a-tabs>
      </a-form-model>
    </j-modal>
    <order-base-modal ref="modalForm" @ok="modalFormOk"></order-base-modal>
  </a-card>
</template>

<script>
import {
  httpAction,
  getAction,
  putAction,
  putParamsAction
} from '@/api/manage'
import '@/assets/less/TableExpand.less'
import {
  mixinDevice
} from '@/utils/mixin'
import {
  JeecgListMixin
} from '@/mixins/JeecgListMixin'
import OrderBaseModal from './modules/OrderBaseModal'
import {
  filterMultiDictText
} from '@/components/dict/JDictSelectUtil'
import { postAction } from '../../api/manage'

export default {
  name: 'OrderBaseList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    OrderBaseModal
  },
  data() {
    return {
      shipped: { expressCompany: '', expressNo: '' },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      model: {},
      goods: false,
      width: 1400,
      verifyCode: '',
      visibleVer: false,
      showForcedOk: false,
      showForcedRefund: false,
      refundRemark: '',
      okRemark: '',
      shopList: [],
      selectedRowOrderNos: [],
      description: '商品订单信息表管理页面',
      // 表头
      columns: [{
        title: '#',
        dataIndex: '',
        key: 'rowIndex',
        width: 60,
        align: 'center',
        customRender: function (t, r, index) {
          return parseInt(index) + 1
        }
      },

      // {
      //   title: '分站',
      //   align: 'center',
      //   dataIndex: 'siteId_dictText'
      // },
      // {
      //   title: '商家名称',
      //   align: 'center',
      //   dataIndex: 'merchantId_dictText'
      // },
      {
        title: '店铺名称',
        align: 'center',
        dataIndex: 'shopId_dictText'
      },
      // {
      //   title: '会员',
      //   align: 'center',
      //   dataIndex: 'memberId_dictText'
      // },
      {
        title: '订单号',
        align: 'center',
        dataIndex: 'orderNo'
      },
      // {
      //   title: '订单类型',
      //   align: 'center',
      //   dataIndex: 'type_dictText'
      // },
      {
        title: '订单状态',
        align: 'center',
        dataIndex: 'status_dictText'
      },
      // {
      //   title: '来源',
      //   align: 'center',
      //   dataIndex: 'source_dictText'
      // },
      {
        title: '订单价格',
        align: 'center',
        dataIndex: 'payMoney'
      },
      {
        title: '下单时间',
        align: 'center',
        dataIndex: 'createTime'
      },
      // {
      //   title: '订单来源',
      //   align: 'center',
      //   dataIndex: 'source_dictText'
      // },
      // {
      //   title: '下单用户',
      //   align: 'center',
      //   dataIndex: 'memberId_dictText'
      // },
      // {
      //   title: '联系方式',
      //   align: 'center',
      //   dataIndex: 'createBy'
      // },
      // {
      //   title: '订单备注',
      //   align: 'center',
      //   dataIndex: 'remark'
      // },
      {
        title: '操作',
        dataIndex: 'action',
        align: 'center',
        fixed: 'right',
        width: 147,
        scopedSlots: {
          customRender: 'action'
        }
      }
      ],
      url: {
        list: '/order/orderBase/list',
        delete: '/order/orderBase/delete',
        deleteBatch: '/order/orderBase/deleteBatch',
        exportXlsUrl: '/order/orderBase/exportXls',
        importExcelUrl: 'order/orderBase/importExcel',
        getShopListUrl: '/shop/shopBasic/selectShopList',
        verifyOrder: '/order/orderBase/verifyOrder',
        forceComplete: '/order/orderRefund/forceOrderComplete',
        forceRefund: '/order/orderRefund/forceOrderRefund',
        selectOrderInfo: '/order/orderBase/selectOrderInfo',
        shipped: '/order/orderBase/shipped',
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.getShopListUrl()
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    shippedOk() {
      if (!this.shipped.expressCompany) {
        this.$message.warning('请输入快递公司')
        return
      }
      if (!this.shipped.expressNo) {
        this.$message.warning('请输入快递单号')
        return
      }
      this.shipped.orderId = this.model.id
      postAction(this.url.shipped, this.shipped).then(res => {
        if (res.success) {
          this.$message.success('发货成功')
          this.goods = false
          this.searchQuery()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    showm(e) {
      this.model = e
      console.log(e)
      this.goods = true
      getAction(this.url.selectOrderInfo + '/' + e.id).then(res => {
        if (res.success) {
          let result = res.result
          console.log(result)
          if (null != result.orderLineList && result.orderLineList.length > 0) {
            this.model.orderLineList = result.orderLineList
          }
          if (null != result.orderPay) {
            this.model.orderPay = result.orderPay
          }
          if (null != result.orderDelivery) {
            this.model.orderDelivery = result.orderDelivery
          }
          if (null != result.orderPromoList && result.orderPromoList.length > 0) {
            this.model.orderPromoList = result.orderPromoList
          }
          if (null != result.ccptOrderInfo) {
            this.model.ccptOrderInfo = result.ccptOrderInfo
          }
        }
      })
    },
    // 强制完成
    forceComplete() {
      this.showForcedOk = true;
    },
    forceRefund() {
      this.showForcedRefund = true;
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRowOrderNos = selectionRows.map(item => {
        return item.orderNo
      })
    },
    // 确定强制完成
    forceOk() {
      if (!this.okRemark) {
        this.$message.error('请填写备注');
        return;
      }
      putParamsAction(this.url.forceComplete + '?orderNos=' + this.selectedRowOrderNos + '&remark=' + this.okRemark).then(res => {
        console.log(res);
        if (res.success) {
          this.$message.success('强制完成成功');
          this.handleCancel();
        }
      })
    },
    forceRefundOk() {
      if (!this.refundRemark) {
        this.$message.error('请填写备注');
        return;
      }
      putParamsAction(this.url.forceRefund, {
        orderNos: this.selectedRowOrderNos,
        remark: this.refundRemark
      }).then(res => {
        console.log(res);
        if (res.success) {
          this.$message.success('强制退款成功');
          this.handleCancel();
        }
      })
    },
    getShopListUrl() {
      this.shopList = [];
      getAction(this.url.getShopListUrl).then(res => {
        if (res.success) {
          let result = res.result
          result.forEach((e) => {
            this.shopList.push(e)
          })
        }
      })
      console.log(this.shopList)

    },
    initDictConfig() { },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({
        type: 'string',
        value: 'siteId',
        text: '分站',
        dictCode: 'mk_site_basic,name,id'
      })
      fieldList.push({
        type: 'string',
        value: 'merchantId',
        text: '商家',
        dictCode: 'mk_merchant_basic,name,id'
      })
      fieldList.push({
        type: 'string',
        value: 'shopId',
        text: '店铺',
        dictCode: 'mk_shop_basic,name,id'
      })
      fieldList.push({
        type: 'string',
        value: 'memberId',
        text: '会员',
        dictCode: 'mk_member_basic,name,id'
      })
      fieldList.push({
        type: 'string',
        value: 'orderNo',
        text: '订单号',
        dictCode: ''
      })
      fieldList.push({
        type: 'int',
        value: 'type',
        text: '类型1：外卖 2：自提 3：店内',
        dictCode: 'order_type'
      })
      fieldList.push({
        type: 'string',
        value: 'status',
        text: '状态',
        dictCode: 'order_status'
      })
      fieldList.push({
        type: 'int',
        value: 'source',
        text: '来源1:微信小程序 2：支付宝小程序 3：安卓APP 4:IOSAPP',
        dictCode: 'order_source'
      })

      fieldList.push({
        type: 'string',
        value: 'ccptOrderStatus',
        text: '订单号',
        dictCode: 'ccptOrderStatus'
      })
      fieldList.push({
        type: 'string',
        value: 'ccptStaffName',
        text: '订单号',
        dictCode: ''
      })
      fieldList.push({
        type: 'string',
        value: 'ccptStaffMobile',
        text: '订单号',
        dictCode: ''
      })
      this.superFieldList = fieldList
    },
    showModal() {
      this.visibleVer = true;
    },
    handleCancel(e) {
      console.log(e)
      this.verifyCode = '';
      this.visibleVer = false;
      this.showForcedOk = false;
      this.showForcedRefund = false;
      this.okRemark = '';
      this.refundRemark = "";
    },
    handleOk(value) {
      console.log(value)
      getAction(this.url.verifyOrder, {
        code: value
      }).then(res => {
        if (res.success) {
          that.$message.success("核销成功")
        } else {
          that.$message.warning(res.message)
        }
      })

    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
/deep/ .ant-tabs {
  height: 300px !important;
}
</style>